package com.example.micrweb.front.controller;

import com.example.common.constants.RedisKey;
import com.example.common.enums.RCode;
import com.example.common.util.CommonUtil;
import com.example.micrweb.front.service.SmsService;
import com.example.micrweb.front.view.RespResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.web.bind.annotation.*;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/7/15 下午 03:30
 * @version: v1.0
 */
@Tag(name="短信业务")
@RestController
@RequestMapping("/v1/sms")
public class SmsController extends BaseController{

    @Resource
    SmsService smsService;

    @Operation(summary = "发送短信" )
    @GetMapping("/code/register")
    /*发送注册信息短信*/
    public RespResult sendCodeRegister(@RequestParam("phone") String phone){
        RespResult result = RespResult.fail();
        if (CommonUtil.checkPhone(phone)){
            // 判断redis中是否有这个号码
            String key = RedisKey.KEY_SMS_CODE_REG + phone;
            if (stringRedisTemplate.hasKey(key)) {
                result.setRCode(RCode.SUCC);
            }else {
                // 发送短信
                boolean sendSms = smsService.sendSms(phone);
                if (sendSms){
                    result.setRCode(RCode.SUCC);
                }
            }
        }else {
                result.setRCode(RCode.REQUEST_PHONE_FORMAT_ERR);
        }
        return result;
    }
}
